package base

import (
	"bytes"
	"fmt"
	"reflect"
	"strconv"
	"strings"
)

func ArrayToStringBySprint(array ...interface{}) string {
	return strings.Replace(strings.Trim(fmt.Sprint(array), "[]"), " ", ",", -1)
}

func ArrayToStringByBuffer(array []interface{}) string {
	var buffStr bytes.Buffer
	for _, v := range array {
		buffStr.WriteString(interfaceToString(v))
		buffStr.WriteString(",")
	}
	return buffStr.String()
}

func interfaceToString(inter interface{}) (res string) {
	switch inter.(type) {
	case string:
		res = inter.(string)
	case int:
		res = strconv.Itoa(inter.(int))
	case float64:
		res = strconv.FormatFloat(inter.(float64), 'f', 6, 64)
	default:
		fmt.Println(reflect.TypeOf(inter))
	}
	return
}

func JoinStringByAddSymbol(buf bytes.Buffer) string {
	//imageData := "data:" + "image/png" + ";base64," + base64.StdEncoding.EncodeToString(buf.Bytes())
	imageData := "data:" + "image/png" + ";base64," + buf.String()
	return imageData
}

func JoinStringByBuffer(buf bytes.Buffer) string {
	var buffStr bytes.Buffer
	buffStr.WriteString("data:image/png;base64,")
	//buffStr.WriteString(base64.StdEncoding.EncodeToString(buf.Bytes()))
	buffStr.Write(buf.Bytes())
	return buffStr.String()
}

func JoinStringBySprintf(buf bytes.Buffer) string {
	//img := fmt.Sprintf("data:%s;base64,%s", "image/png", base64.StdEncoding.EncodeToString(buf.Bytes()))
	img := fmt.Sprintf("data:%s;base64,%s", "image/png", buf.Bytes())
	return img
}
